package com.sun.officeoa.controller;

import com.sun.officeoa.bean.Employee;
import com.sun.officeoa.bean.Meeting;
import com.sun.officeoa.service.EmployeeService;
import com.sun.officeoa.service.MeetingService;
import com.sun.officeoa.util.StringUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping(value = "/office-oa")
public class LoginController {

    @Resource
    private EmployeeService employeeService;

    @Resource
    private MeetingService meetingService;


    /**
     * @Author SUN-JM
     * @Description //TODO 登陆验证
     * @Date 18:47 2019/2/28
     * @Param [accountname, password, req, resp]
     * @return org.springframework.web.servlet.ModelAndView
     **/
    @RequestMapping("/loginUser")
    public ModelAndView login(@RequestParam("accountname") String accountname, @RequestParam("password") String password, HttpServletRequest req, HttpServletResponse resp){
        ModelAndView mv = new ModelAndView();
        int login = employeeService.login(accountname, password);
        ServletContext sc = req.getServletContext();
        Object visitcount = sc.getAttribute("vc");
        if (visitcount == null) {
            visitcount = "0";
        }
        int vc = Integer.parseInt(visitcount.toString()) + 1;
        sc.setAttribute("vc", vc);
        if (login == 0) {
            mv.addObject("error", "用户待审批，请稍候");
            mv.setViewName("/login");
        } else if (login == 1) {
            req.getSession().setAttribute("loginUser", employeeService.getLoginUser());
            mv.setViewName("redirect:/office-oa/notifications");
        } else if (login == 2) {
            mv.addObject("error", "用户审批未通过，请重新注册");
            mv.setViewName("/login");
        } else if (login == 3) {
            mv.addObject("error", "用户名或者密码输入错误，请重新登录");
            mv.setViewName("/login");
        } else if (login == -1) {
            mv.addObject("error", "账号已关闭，登陆失败，请联系管理员");
            mv.setViewName("/login");
        }
        return mv;
    }
    /**
     * @Author SUN-JM
     * @Description //TODO 登陆
     * @Date 18:47 2019/2/28
     * @Param []
     * @return java.lang.String
     **/
    @RequestMapping("/login")
    public String login(){
        return "login";
    }
    /**
     * @Author SUN-JM
     * @Description //TODO 最新通知
     * @Date 18:47 2019/2/28
     * @Param [req, resp]
     * @return org.springframework.web.servlet.ModelAndView
     **/
    @RequestMapping("/notifications")
    public ModelAndView notifications(HttpServletRequest req, HttpServletResponse resp){
        ModelAndView modelAndView = new ModelAndView();
        int loginEmpId = ((Employee) req.getSession().getAttribute("loginUser")).getEmployeeid();
        List<Meeting> mt7 = meetingService.getMeeting7Days(loginEmpId);
        List<Meeting> cm = meetingService.getCanceledMeeting(loginEmpId);
        modelAndView.addObject("mt7", mt7);
        modelAndView.addObject("cm", cm);
        modelAndView.setViewName("/notifications");

        return modelAndView;

    }

    @RequestMapping("/changepassword")
    public ModelAndView changepassword(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/changepassword");
        return modelAndView;
    }
    /*
     * @Author SUN-JM
     * @Description //TODO 注销登陆信息
     * @Date 17:16 2019/3/6
     * @Param [request]
     * @return java.lang.String
     **/
    @RequestMapping("/exitlogin")
    public String exitlogin(HttpServletRequest request){
        HttpSession session = request.getSession(false);//防止创建Session
        if(session == null){
            return "login";
        }
        //清空session的用户信息
        session.removeAttribute("loginUser");
        return "login";
    }
    @RequestMapping("/updateUser")
    public ModelAndView updateUser(HttpServletRequest request){
        ModelAndView modelAndView = new ModelAndView();
        String passoword = request.getParameter("confirm");
        String origin = request.getParameter("origin");//页面老密码
        HttpSession httpSession =  request.getSession();
        Employee employee = (Employee)httpSession.getAttribute("loginUser");
        int emId =  employee.getEmployeeid();
        String passwordOld = employee.getPassword();//缓存密码
        if(!origin.equals(passwordOld)){
            modelAndView.addObject("error","旧密码输入错误");
            modelAndView.setViewName("/login");
            return modelAndView;
        }
        int ss = employeeService.updateUser(emId,passoword);
        if(ss>0){
            modelAndView.setViewName("redirect:/office-oa/notifications");
            return modelAndView;
        }else{
            modelAndView.addObject("error","修改失败");
        }
        modelAndView.setViewName("/login");
        return modelAndView;
    }
}
